package com.xi.springboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xi.springboot.annotation.SysLog;
import com.xi.springboot.entity.Record;
import com.xi.springboot.entity.Url;
import com.xi.springboot.entity.UrlVo;
import com.xi.springboot.entity.User;
import com.xi.springboot.mapper.UrlMapper;
import com.xi.springboot.service.UrlService;
import com.xi.springboot.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: xjs
 * @Date: 2022/11/19/10:59
 * @Description:
 */
@Service
public class UrlServiceImpl implements UrlService {
    @Autowired
    UrlMapper urlMapper;
    @SysLog("业务层:绑定用户和短链接")
    @Override
    public Boolean bindUrl(String url, String shortUrl,Long userId) {
        Url newUrl = new Url();
        newUrl.setOriginUrl(url);
        User user =new User();
        user.setId(userId);
//        newUrl.setUser(user);
        newUrl.setShortUrlId(shortUrl);
        return urlMapper.insert(newUrl)>0;
    }

    @SysLog("业务层:生成短链接")
    @Override
    public String getShortenUrl(HttpServletRequest request) {
        return UrlUtils.getUrl();
    }

    @SysLog("业务层:根据短链接获取原链接")
    @Override
    public String getOriginUrl(String separate, String shortUrl) {
        LambdaQueryWrapper<Url> mapper = new LambdaQueryWrapper<>();
        mapper.eq(Url::getShortUrlId,shortUrl);
         Url url = urlMapper.selectOne(mapper);
         url.setVisits(url.getVisits() + 1);
         url.setLastVisit(new Date());
         urlMapper.update(url,mapper);
        return urlMapper.selectOriginUrlBySeparateAndShortUrl(separate,shortUrl);
    }

    @SysLog("业务层:(用户逻辑)根据用户id获取其生成的短链接")
    @Override
    public ArrayList<Url> getUrls(Long userId) {
        LambdaQueryWrapper<Url> mapper = new LambdaQueryWrapper<>();
        mapper.eq(Url::getUserId,userId);
        return (ArrayList<Url>) urlMapper.selectList(mapper);
//        return (ArrayList<Url>) urlMapper.selectUrlById(userId);
    }

    @SysLog("业务层:(游客逻辑)存储游客生成的短链接")
    @Override
    public HashMap<String,String> cashUrl(String url, String shortUrl) {
        HashMap<String,String> urls = new HashMap<>();
        urls.put(url,shortUrl);
        return urls;
    }

    @SysLog("业务层:(管理员业务)获取全部的链接信息")
    @Override
    public IPage<Url> selectALlUrls(Integer page) {
        IPage<Url> ToPage =new Page<>(page,5);
        IPage<Url> urlIPage = urlMapper.selectPage(ToPage,null);
        System.out.println(urlIPage.getRecords());
        System.out.println(urlIPage.getPages());
        System.out.println(urlIPage.getTotal());
        return urlIPage;
    }
    @SysLog("业务层:(管理员业务)获取全部的链接信息")
    @Override
    public IPage<UrlVo> selectByPage(Integer page) {
        Page<UrlVo> voPage = new Page<>(page,5);
        IPage<UrlVo> urlVoIPage = urlMapper.selectByPage(voPage);
        return urlVoIPage;
    }

//    @Override
//    public IPage<Record> selectByPage(Integer page) {
//        IPage<Record> ToPage =new Page<>(1,5);
//        IPage<Record> urlIPage = urlMapper.selectPage(ToPage,null);
//        System.out.println(urlIPage.getRecords());
//        System.out.println(urlIPage.getPages());
//        System.out.println(urlIPage.getTotal());
//        return urlIPage;
//    }

//    @Override
//    public IPage<?> getPageById(IPage<?> page) {
//
//        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
//
//        Page<Record> page1 = urlMapper.selectPage(page, queryWrapper);
//
////        T = Record
////        E = Page object, E is IPage<Record> chil
////
//
////        <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
//
//        return null;
//    }

//    @Override
//    public List<Record> getPageById(IPage<Record> page) {
//       IPage<Record> selectPage = urlMapper.selectPage(page,null);
//        System.out.println(selectPage);
//        return null;
//    }

}
